#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/8 17:13
# @USER    : Shengji He
# @File    : RemoveDuplicatesSortedArr.py
# @Software: PyCharm
# @Version  : Python-
# @TASK:
from typing import List


class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        """
        Given a sorted array nums, remove the duplicates in-place such that each element appear only once and
        return the new length.

        Do not allocate extra space for another array, you must do this by modifying the input array in-place
        with O(1) extra memory.

        Example 1:

            Given nums = [1,1,2],

            Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

            It doesn't matter what you leave beyond the returned length.

        Example 2:

            Given nums = [0,0,1,1,1,2,2,3,3,4],

            Your function should return length = 5, with the first five elements of nums being modified
            to 0, 1, 2, 3, and 4 respectively.

            It doesn't matter what values are set beyond the returned length.

        Clarification:

            Confused why the returned value is an integer but your answer is an array?

            Note that the input array is passed in by reference, which means modification to the input array will be
            known to the caller as well.

        :param nums: List[int]
        :return: int
        """
        if len(nums) < 2:
            return len(nums)
        # length = 1
        # while length < len(nums):
        #     if nums[length] == nums[length - 1]:
        #         nums.pop(length)
        #     else:
        #         length += 1
        # return length
        p = 0
        q = 1
        while q < len(nums):
            if nums[q] == nums[p]:
                q += 1
            else:
                nums[p + 1] = nums[q]
                p += 1
                q += 1
        # 根据我们的定义，数组长度为末尾元素索引加上1
        nums = nums[:p + 1]
        return p + 1


if __name__ == '__main__':
    S = Solution()
    nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
    # nums = [1, 1, 2]
    print(S.removeDuplicates(nums))
    print(nums)
    print('done')
